package com.wayin.common.utils;

import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

public class PicUtils {

    public String putPic(MultipartFile file) throws Exception {
        String path = ClassUtils.getDefaultClassLoader().getResource("static/face").getPath();
        String new_fileName = UUID.randomUUID() + file.getOriginalFilename();
        String url_path = "/" + new_fileName;
        String savePath = path + "/" + url_path;
        File dest = new File(savePath);
        if (!dest.exists()){
            dest.mkdirs();
//            dest.createNewFile();
        }
        try{
            file.transferTo(dest);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("上传失败");
            return null;
        }
        System.out.println("上传成功");
        return url_path;
    }

    public String saveUploadedFiles(final MultipartFile file) throws IOException {
        String path1 = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        final byte[] bytes = file.getBytes();
        String new_fileName = UUID.randomUUID() + file.getOriginalFilename();
        String url_path = "/" + new_fileName;
        String savePath = path1 + url_path;
        File dest = new File(savePath);
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
            dest.createNewFile();
        }
        try{
            file.transferTo(dest);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("上传失败");
            return null;
        }
        System.out.println("上传成功");
        return url_path;
//        final Path path = Paths.get(savePath);
//        Files.write(path, bytes);
//        return savePath;
    }

    // 接收app的base64格式图片
    public String saveBase64(String base64){
        try {
            base64 = base64.replace(" ", "+");
            BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64);
            //生成图片名称
            String imageUrl = "outside/" + UUID.randomUUID().toString().replace("-", "") + ".jpg";
            String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
            for (int i = 0; i < bytes1.length; i++) {
                if (bytes1[i] < 0) {
                    //调整异常数据
                    bytes1[i] += 256;
                }
            }
            File file = new File(path, imageUrl);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            out.write(bytes1);
            out.flush();
            out.close();
            return imageUrl;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 接收app的base64格式图片
    public String saveBase642(String base64){
        try {
            base64 = base64.replace(" ", "+");
            BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64);
            //生成图片名称
            String imageUrl = "/" + UUID.randomUUID().toString().replace("-", "") + ".jpg";
            String path = ClassUtils.getDefaultClassLoader().getResource("static/face").getPath();
            for (int i = 0; i < bytes1.length; i++) {
                if (bytes1[i] < 0) {
                    //调整异常数据
                    bytes1[i] += 256;
                }
            }
            File file = new File(path, imageUrl);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            out.write(bytes1);
            out.flush();
            out.close();
            return imageUrl;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 迭代删除文件夹
     * @param dirPath 文件夹路径
     */
    public static void deleteDir(String dirPath){
        // 读取
        File file = new File(dirPath);
        // 判断是否是文件夹
        if(file.isFile()){
            // 删除
            file.delete();
        }else{
            // 获取文件
            File[] files = file.listFiles();
            if(files == null){
                // 删除
                file.delete();
            }else{
                // 循环
                for (int i = 0; i < files.length; i++){
                    deleteDir(files[i].getAbsolutePath());
                }
                // 删除
                file.delete();
            }
        }
    }

}
